﻿using System.Threading.Tasks;
using Aierp.SystemEntities.System;
using Aierp.SystemRepository.Implements;
using Aierp.Utility;
using Aierp.Utility.Core;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;

namespace Aierp.Controllers
{
    /// <summary>
    /// 按钮管理
    /// 返回结果值--重复返回-9，查询不到数据返回-2
    /// </summary>
    [Route("api/[controller]")]
    public class ButtonsManageController : ControllerBase
    {
        ButtonsRepository ButtonsRepository { get; set; }
        SysVisitor SysVisitor { get; set; }
        public ButtonsManageController(ButtonsRepository buttonsRepository, SysVisitor sysVisitor)
        {
            ButtonsRepository = buttonsRepository;
            SysVisitor = sysVisitor;
        }

        /// <summary>
        /// 获取单个按钮
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        // GET api/values/5
        [HttpGet("{id}")]
        public async Task Get(int id)
        {
            var model = ButtonsRepository.FindById(id);
            if (model == null)
            {
                await WriteFailJson();
                return;
            }
            await WriteResultJson(model);
        }

        /// <summary>
        /// 获取按钮列表
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetList")]
        public async Task GetList()
        {
            var pageParam = this.GetPageParam();
            await Response.WriteAsync(ButtonsRepository.GetList(pageParam.Order, pageParam.Limit, pageParam.Offset, pageParam.SearchModel));
        }

        /// <summary>
        /// 添加按钮
        /// </summary>
        /// <returns></returns>
        [HttpPost("Add")]
        public async Task Add()
        {
            //获取按钮
            var model = GetModel<Sys_Buttons>();

            if (model.ButtonText.IsEmpty() || model.ButtonTag.IsEmpty())
            {
                await this.WriteSpecReasonJson("添加失败，错误信息如下：标题或编码不能为空！");
                return;
            }

            var result = ButtonsRepository.Add(model);
            if (result > 0)
            {
                await this.WriteSucessJson();
                return;
            }

            //处理错误信息
            switch (result)
            {
                case -9:
                    await this.WriteSpecReasonJson("添加失败，错误信息如下：系统中已经存在这个按钮！");
                    return;
                default:
                    await this.WriteSpecReasonJson("添加失败,请联系管理员！");
                    break;
            }
        }
        /// <summary>
        /// 更新按钮
        /// </summary>
        /// <returns></returns>
        [HttpPost("Update")]
        public async Task Update()
        {
            //获取按钮
            var model = GetModel<Sys_Buttons>();

            if (model.ButtonText.IsEmpty() || model.ButtonTag.IsEmpty())
            {
                await this.WriteSpecReasonJson("更新失败，错误信息如下：标题或编码不能为空！");
                return;
            }

            var result = ButtonsRepository.Update(model);
            if (result > 0)
            {
                await this.WriteSucessJson();
                return;
            }

            //处理错误信息
            switch (result)
            {
                case -2:
                    await this.WriteSpecReasonJson("更新失败，错误信息如下：系统中找不到这个按钮！");
                    return;
                default:
                    await this.WriteSpecReasonJson("更新失败,请联系管理员！");
                    break;
            }
        }

        /// <summary>
        /// 删除按钮--物理删除
        /// </summary>
        /// <param name="id"></param>
        // DELETE api/values/5
        [HttpDelete("{id}")]
        public async Task Delete(int id)
        {
            if (id == 0)
            {
                await this.WriteSpecReasonJson("标题或编码不能为空！");
                return;
            }

            var result = ButtonsRepository.Remove(id);
            if (result > 0)
            {
                await this.WriteSucessJson();
                return;
            }

            //处理错误信息
            switch (result)
            {
                case -2:
                    await this.WriteSpecReasonJson("删除失败，错误信息如下：系统中找不到这个按钮！");
                    return;
                default:
                    await this.WriteSpecReasonJson("删除失败,请联系管理员！");
                    break;
            }
        }
    }
}
